package com.jiang.controller;

import com.jiang.bean.User;
import com.jiang.service.UserService;
import com.jiang.utils.AjaxResult;
import com.jiang.utils.VisterLoginUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by dllo on 18/4/23.
 */
@Controller
public class UserCotroller {
    @Resource
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/userlogin")
    public AjaxResult login(@RequestParam(value = "username") String username,
                            @RequestParam(value = "password") String password) {
        AuthenticationToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return AjaxResult.getOk("登陆成功");
        } catch (Exception e) {
            return AjaxResult.getNo("账号密码错误");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getname")
    public AjaxResult getname() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        String name = user.getUsername();
        return AjaxResult.getOk(name);
    }

    @RequestMapping(value = "/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "/getInternetSong";
    }

    /**
     * 普通用户注册
     */
    @RequestMapping(value = "/register")
    public AjaxResult regist(@RequestParam(value = "username") String username,
                             @RequestParam(value = "password") String password,
                             @RequestParam(value = "name") String name) {
        System.out.println(username + "      " + password + "   " + name);
        User user = new User();
        user.setLoginname(username);
        user.setLoginpwd(password);
        user.setUsername(name);
        user.setRoleid(2);
        userService.register(user);
        return AjaxResult.getOk("成功");
    }

    /**
     * 游客登录
     */
    @RequestMapping(value = "/visterlogin")
    public String visterLogin() {
        //注册游客账号
        User user = new User();
        //利用工具类创建账号密码
        String loginName = VisterLoginUtil.getLoginName();
        String loginPwd = VisterLoginUtil.getLoginPwd();
        user.setUsername("游客" + loginName);
        user.setLoginname(loginName);
        user.setLoginpwd(loginPwd);
        //将角色设置为游客
        user.setRoleid(1);
        //注册
        userService.register(user);
        //利用刚才注册的账号密码进行登录
        //创建token
        AuthenticationToken token = new UsernamePasswordToken(loginName, loginPwd);
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        return "/getInternetSong";
    }
}
